<template>
	<view>
		<view class="null-cart" v-if="isNull">
			<image src="../../static/cart_empty@2x.png" mode=""></image>
			<view class="text">
				空空如也~
			</view>
		</view>
		<view v-else>
			<!-- 商品收货地址 -->
			<my-address></my-address>
			<!-- 商品列表的标题区域 -->
			<view class="cart-title">
				<!-- 左侧的图标 -->
				<uni-icons type="shop" size="18"></uni-icons>
				<!-- 右侧的文本 -->
				<text class="cart-title-text">购物车</text>
			</view>

			<!-- 循环渲染购物车中的商品信息 -->
			<uni-swipe-action>
				<view v-for="(item,index) in cart" :key="index" @click="gotoDetail(item)">
					<uni-swipe-action-item :auto-close="autoClose" :right-options="options"
						@click="swipeItemClickHandler(item)">
						<my-goods :goods="item" :showNumber="true" :showRadio="true" @radio-change="radioChangeHandler"
							@number-change="numberChangeHandler"></my-goods>
					</uni-swipe-action-item>
				</view>
				<view class="zhanwei">

				</view>
			</uni-swipe-action>
			<!-- 结算 -->
			<my-settle></my-settle>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		onShow() {
			this.setBadge(this.total)
		},
		watch: {
			total(newVal) {
				this.setBadge(newVal)
			}
		},
		computed: {
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['isNull', 'total'])
		},
		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#C00000'
					}
				}]
			};
		},
		methods: {
			...mapMutations('m_cart', ['updataGoodsState', 'updataGoodsNum', 'updataGoods', 'setBadge']),
			radioChangeHandler(e) {
				// console.log(e);
				this.updataGoodsState(e)
			},
			numberChangeHandler(e) {
				// console.log(e);
				this.updataGoodsNum(e)
			},
			swipeItemClickHandler(goods) {
				this.updataGoods(goods)
			},
			// 跳转至详情列表
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
				// console.log(item)
			}
		}
	}
</script>

<style lang="scss">
	.null-cart {
		display: flex;
		flex-direction: column;
		align-items: center;

		image {
			margin-top: 150px;
			width: 70px;
			height: 70px;
		}

		.text {
			font-size: 12px;
			color: #c6c6c6;
			margin-top: 10px;
		}
	}

	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		padding-left: 5px;
		border-bottom: 1px solid #efefef;

		.cart-title-text {
			font-size: 14px;
			margin-left: 10x;
		}
	}

	.zhanwei {
		height: 50px;
	}
</style>
